// mircoApp/numberCollection/orderList/orderList.js
const app = getApp();
import Dialog from '../../../dist/dialog/dialog';
const skinBehavior = require("../../../utils/themeSkin/skinBehavior");
Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    tabIndex:0,
    status:0,
    orderTab:[
      {
        name:'全部',
        status:0,
        page: 1,
        totalPage: 0,
        goodsList:[],
        isLoadMore:true
      },
      {
        name:'待发货',
        status:1,
        page: 1,
        totalPage: 0,
        goodsList:[],
        isLoadMore:true
      },
      {
        name:'待签收',
        status:2,
        page: 1,
        totalPage: 0,
        goodsList:[],
        isLoadMore:true
      },
      {
        name:'已签收',
        status:3,
        page: 1,
        totalPage: 0,
        goodsList:[],
        isLoadMore:true
      }
    ],
    orderList:[
      
    ],
    orderApiArr: [
      "plugin.collection-center.frontend.controllers.order.collection-order-list",
      "plugin.collection-center.frontend.controllers.order.collection-order-list.waitSend",
      "plugin.collection-center.frontend.controllers.order.collection-order-list.waitReceive",
      "plugin.collection-center.frontend.controllers.order.collection-order-list.Completed"
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },
  orderTabOn(event) {
    this.setData({
      tabIndex:event.detail.name,
    })
    if (this.data.orderTab[this.data.tabIndex].page >= 2) { //避免切换到其他分类小于当前页码导致重复加载分页
      this.setData({
        [`orderTab[${this.data.tabIndex}].goodsList`]: []
      })
    }
    wx.pageScrollTo({
      scrollTop: 0,
      duration:500
    })
    if (this.data.orderTab[this.data.tabIndex].goodsList && this.data.orderTab[this.data.tabIndex].goodsList.length === 0) {
      this.data.orderTab[this.data.tabIndex].page = 1;
      this.orderLists();
    }
  },
  orderLists() {
    let api = this.data.orderApiArr[this.data.tabIndex];
    let query = {
      page: this.data.orderTab[this.data.tabIndex].page,
      status:this.data.status
    }
    let urlStr = app.getNetAddresss(api);
    app._postNetWork({
      url: urlStr,
      data:query,
      success:  (resdata) => {
        let res = resdata.data;
        let data = res.data;
        wx.hideLoading();
        wx.hideNavigationBarLoading();//隐藏导航条加载动画
        //停止下拉刷新
        wx.stopPullDownRefresh();
        if (res.result == 1) {
          this.data.orderTab[this.data.tabIndex].totalPage = data?.last_page;
          this.data.orderTab[this.data.tabIndex].isLoadMore = true;
          if (!this.data.orderTab[this.data.tabIndex].totalPage) {
            this.data.orderTab[this.data.tabIndex].page = 0;
          }
          this.setData({
            [`orderTab[${this.data.tabIndex}].goodsList`]:data.data
          })
        } else {
          this.data.orderTab[this.data.tabIndex].page = this.data.orderTab[this.data.tabIndex].page - 1;
          this.data.orderTab[this.data.tabIndex].isLoadMore = false;
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  orderstateOn(event) {
    let params = event.currentTarget.dataset;
    this.getorderState(params);
  },
  getorderState(params) {
    let {value, id, refundid, api,itembtn,item} = params;
    let query = {
      18:`/packageD/member/myOrder/Aftersales/Aftersales?refund_id=${refundid}`,
      13:`/packageD/member/myOrder/refund/refund?order_id=${id}&type=1`,
      41: `${api}`, //客服
    };
    if(value.toString() === '12') {
      Dialog.confirm({
        title: '提示',
        message: '确定删除该订单',
      }).then(() => {
        // on close
        this.deletePay(id,api);
      })
      .catch(() => {
        // on cancel
      });  //删除订单
    } else if(value == '5') {
      Dialog
      .confirm({ message: itembtn.name })
      .then(() => {
        this._confirmOrder_request(id, itembtn);
      })
      .catch(() => {});
    } else if (value == 8 || value == "snatch_regiment_express") {
      this._checklogistics(item, itembtn);
    } else {
      if(query[value]) {
        return wx.navigateTo({
          url: query[value]
        });
      } else {
        console.log("获取未知按钮状态数据失败");
      }
    }
  },
  deletePay(id,api) {
    let urlStr = app.getNetAddresss(api);
    let json = {
      order_id:id
    };
    app._postNetWork({
      url: urlStr,
      data:json,
      success:  (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
          this.data.orderTab[this.data.tabIndex].page = 1;
          this.orderLists();
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  _checklogistics(item, btn) {
    //logistics
    if (item.is_all_send_goods == 0) {
      wx.navigateTo({
        url: `/packageA/member/order/logistics/logistics?id=${item.id}&api=${btn.api}`
      });
    } else {
      wx.navigateTo({
        url: `/packageE/multiplePackages/multiplePackages?id=${item.id}&api=${btn.api}`
      });
    }
  },
  _confirmOrder_request(id,btn){
    let urlStr = app.getNetAddresss(btn.api);
    urlStr += "&order_id=" + id;
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          app.tips(res.msg);
          this.data.orderTab[this.data.tabIndex].page = 1;
          this.orderLists();
        } else {
          app.tips(res.msg);
        }
      },
      fail: (res) => {
        console.log(res);
      },
    });
  },
  getMoreData() {
    this.data.orderTab[this.data.tabIndex].isLoadMore = false;
    if (this.data.orderTab[this.data.tabIndex].page >= this.data.orderTab[this.data.tabIndex].totalPage) {
      return;
    } else {
      this.data.orderTab[this.data.tabIndex].page +=  1;
      let api = this.data.orderApiArr[this.data.tabIndex];
      let urlStr = app.getNetAddresss(api);
      let json = {
        page: this.data.orderTab[this.data.tabIndex].page,
      };
      app._getNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          let res = resdata.data;
          let list = res.data.data;
          let tabIndex = this.data.tabIndex;
          if (res.result == 1) {
            this.data.orderTab[tabIndex].isLoadMore = true;
            this.setData({
              [`orderTab[${tabIndex}].goodsList`]: this.data.orderTab[tabIndex].goodsList.concat(list),
            })
          } else {
            this.data.orderTab[tabIndex].page = this.data.orderTab[tabIndex].page - 1;
            this.data.orderTab[tabIndex].isLoadMore = false;
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.data.orderTab[this.data.tabIndex].page = 1;
    this.data.orderTab[this.data.tabIndex].goodsList = [];
    this.data.orderTab[this.data.tabIndex].isLoadMore = true;
    this.orderLists();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.orderTab[this.data.tabIndex].isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})